/**
 * salesSummary.jsp引用
 * @param $
 * @param win
 * @param doc
 */
var url = '/business/salesSummaryServlet';
var sumType = 10;
(function($, win, doc){
	// 查询条件
	var createCondCombox = function(){
		// 分店
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 1;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		var param = {
            shopType:"'中深'",
				zoneCode:''
		}
		$('#shop').combobox({
			prompt:'请输入或选择分店', 
	        url: CONTEXT_PATH + '/ktcount/getShop',
	        queryParams:param,
			method : 'post',
		    valueField: 'SHOP_ID',
		    textField: 'SHOP_NAME',
		    groupField:'SHOP_TYPE',
            multiple:true,
		    loader: Util.combox.loader,
		    groupFormatter: function(group){
				return '<span style="color:red">' + group + '</span>';
			},
		    onClick: function(node) { 
                attributeShow($("#shop").combotree("getValue")); 
            }, 
            filter: function(q, row){ 
                var opts = $(this).combobox('options'); 
                //return row[opts.textField].indexOf(q) == 0; 
                return row[opts.textField].indexOf(q)>-1;//将从头位置匹配改为任意匹配 
            }, 
		    formatter: function (row) {
                var opts = $('#shop').combobox('options');
                '<input type="checkbox" class="combobox-checkbox">' + '全部'
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess: function (val) { //下拉框数据加载成功调用
            	//如果只有一个店铺就默认选中这个店铺
            	 var shopVal = JSON.stringify(val);
            	 var obj = eval('(' + shopVal + ')');
            	 console.log(obj);
            	 var setText = obj[0].SHOP_NAME;
            	 var setVal = obj[0].SHOP_ID;
            	console.log(setVal);
            	if(val.length == '1'){
            		$('#shop').combobox('setText',setText);
            		$('#shop').combobox('setValue',setVal);
            		$('#shop').combobox('disable');
            	}
            	
                var opts = $('#shop').combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true); 
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $('#shop').combobox('options');
                //获取选中的值的values
                $("#shop").val($('#shop').combobox('getValues'));
			   //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $('#shop').combobox('options');
                //获取选中的值的values
                $("#shop").val($('#shop').combobox('getValues'));
              
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            },
		});
		// 供应商
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 3;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#supplier').combobox({
			url: CONTEXT_PATH + '/ktcount/getSupplier',
			queryParams : param,
			method : 'post',
		    valueField: 'ID',
		    textField: 'NAME',
		    multiple:true,
		    loader: Util.combox.loader,
		    filter: function(q, row){ 
                var opts = $(this).combobox('options'); 
                //return row[opts.textField].indexOf(q) == 0; 
                return row[opts.textField].indexOf(q)>-1;//将从头位置匹配改为任意匹配 
            }, 
		    formatter: function (row) {
                var opts = $('#supplier').combobox('options');
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess: function () {  //下拉框数据加载成功调用
                var opts = $('#supplier').combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true); 
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $('#supplier').combobox('options');
                //获取选中的值的values
                $("#supplier").val($('#supplier').combobox('getValues'));
			   //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $('#supplier').combobox('options');
                //获取选中的值的values
                $("#supplier").val($('#supplier').combobox('getValues'));
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            } 
		});
		// 品牌
		param = Param.getReadCmd();
		param[Param.CMDTYPEKEY] = 4;
		param[Param.CMDVALUEKEY] = 1;
		var cond = {};
		param = Param.buildParam(param,cond);
		$('#brand').combobox({
			url: CONTEXT_PATH + '/ktcount/getBrand',
			queryParams : param,
			method : 'post',
		    valueField: 'ID',
		    textField: 'NAME',
		    multiple:true,
		    loader: Util.combox.loader,
		    filter: function(q, row){ 
                var opts = $(this).combobox('options'); 
                //return row[opts.textField].indexOf(q) == 0; 
                return row[opts.textField].indexOf(q)>-1;//将从头位置匹配改为任意匹配 
            }, 
		    formatter: function (row) {
                var opts = $('#brand').combobox('options');
                return '<input type="checkbox" class="combobox-checkbox">' + row[opts.textField]
            },
            onLoadSuccess: function () {  //下拉框数据加载成功调用
                var opts = $('#brand').combobox('options');
                var target = this;
                var values = $(target).combobox('getValues');//获取选中的值的values
                $.map(values, function (value) {
                    var el = opts.finder.getEl(target, value);
                    el.find('input.combobox-checkbox')._propAttr('checked', true); 
                })
            },
            onSelect: function (row) { //选中一个选项时调用
                var opts = $('#brand').combobox('options');
                //获取选中的值的values
                $("#brand").val($('#brand').combobox('getValues'));
			   //设置选中值所对应的复选框为选中状态
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', true);
            },
            onUnselect: function (row) {//不选中一个选项时调用
                var opts = $('#brand').combobox('options');
                //获取选中的值的values
                $("#brand").val($('#brand').combobox('getValues'));
                var el = opts.finder.getEl(this, row[opts.valueField]);
                el.find('input.combobox-checkbox')._propAttr('checked', false);
            }
		});
	};
	/**
	 * 创建条件对象
	 */
	var getCond2 = function(){
		var cond = {};
		//区域
		//var area = $('#area').combobox('getValue');
		var area = $('#areaCode').val();
		cond['ZONE_CODE'] = Param.buildParamItem('IN', area, Param.SQLType.VARCHAR); 
		return cond;
	}
	var getCond = function() {
		var cond = {};
		// 时间
		var startTime = $('#startTime').datebox('getValue');
		cond['startTime'] = Param.buildParamItem('=', startTime, Param.SQLType.VARCHAR); 
		var endTime = $('#endTime').datebox('getValue');
		cond['endTime'] = Param.buildParamItem('=', endTime, Param.SQLType.VARCHAR); 
		//区域
		//var areas = $('#area').combobox('getValues');
		var area = $('#areaCode').val();
		
		if(area == ""){
			area = '-'

		}
		cond['area'] = Param.buildParamItem('=', area, Param.SQLType.VARCHAR); 
		
		//分店
		var shopIds = $('#shop').combobox('getValues');

		var shop = shopIds.toString();

		cond['shop'] = Param.buildParamItem('=', shop, Param.SQLType.VARCHAR); 
		
		//商品类型
		//var goodsType = $('#goodsType').combobox('getValue');
		var goodsType = $('#typeId').val();
		if(goodsType == ""){
			goodsType = '-'

		}
		cond['goodsType'] = Param.buildParamItem('=', goodsType, Param.SQLType.VARCHAR); 
		//品牌
//		var brandIds = $('#brand').combobox('getValues');

//		var brand = brandIds.toString();

//		cond['brand'] = Param.buildParamItem('=', brand, Param.SQLType.VARCHAR); 
		//供应商
		var supplier = $('#supplier').combobox('getValue');
		cond['supplier'] = Param.buildParamItem('=', supplier, Param.SQLType.VARCHAR); 
		//货号
		var artno = $('#artno').val();
		cond['artno'] = Param.buildParamItem('=', '%'+artno+'%', Param.SQLType.VARCHAR); 
		//自编码
		var selfEncode = $('#selfEncode').val();
		if(selfEncode==""){
			selfEncode = "-";
		}
		cond['selfEncode'] = Param.buildParamItem('=', '%'+selfEncode+'%', Param.SQLType.VARCHAR); 
		return cond;
	};
	// 查询
	var onQuery = function(){
		//获取参数
		var startTime = $('#startTime').datebox('getValue');
		var endTime = $('#endTime').datebox('getValue');
		var area = $('#areaCode').val();
		var shopIds = $('#shop').combobox('getValues');
		if(shopIds[0] == '-'){
			shopIds.splice(0,1);
		}
		var shopIds = shopIds.toString();
		var goodsType = $('#typeId').val();
//		var brandIds = $('#brand').combobox('getValues');
//		if(brandIds[0] == '-'){
//			brandIds.splice(0,1);
//		}
//		var brandIds =brandIds.toString();
		var supplier = $('#supplier').combobox('getValue');
		var artno = $('#artno').val();
		var selfEncode = $('#selfEncode').val();
		var sumType = $("input[name='sumType']:checked").val();
        var filters = {
        		startTime:startTime,
        		endTime:endTime,
        		area:area,
        		shop:shopIds,
        		goodsType:goodsType,
//        		brand:brandIds,
        		supplier:supplier,
        		artno:artno,
        		selfEncode:selfEncode,
        		sumType:sumType,
            shopType:"'中深'",
        };
        var grid_data = {'filters': JSON.stringify(filters)};
		var cond = getCond();
		if(!Util.nvl(cond) && cond == false){
			return ;
		}
		var param = Param.getReadCmd();
		var dgCol;
		if(sumType==10){
			param[Param.CMDTYPEKEY] = 0;
			dgCol = [[
				    {field: '_rn',title: '行号',align: 'center',width: 30
			    	,formatter: function(value, row, index) {
			    		return index+1;
			    	}
					},
				    {field: 'PRODUCT_CODE',title: '货号',width: 110,align:'center'},
				    {field: 'SHORT_NAME',title: '品名',width: 220,align:'center',formatter:Util.dgTooltipFormat},
				    {field: 'UNIT',title: '单位',width: 40,align:'center'},
				    {field: 'STATE',title: '商品状态',width: 60,align:'center',
				    	formatter:function(value, row, index){
				    		var state = 0;
				    		if(value == '0'){
				    			state = '未支付';
				    		}else if(value == '1'){
				    			state = '正常';
				    		}else if(value == '-1'){
				    			state = '<span style="color:red">退货</span>';
				    		}
				    		return state;
				    	}
				    	
				    },
				    {field: 'SPEC',title: '规格',width: 70,align:'center'},
				    {field: 'PRICE',title: '零售价',width: 50,align:'center'},
				    {field: 'CST_PRICE',title: '进价',width: 50,align:'center'},
				    {field: 'SALES',title: '销售数量',width: 70,align:'center'},
				    {field: 'SALES_AMONT',title: '销售金额',width: 70,align:'center'},
				    {field: 'RETURN_NUM',title: '退货数量',width: 70,align:'center'},
				    {field: 'RETURN_AMOUNT',title: '退货金额',width: 70,align:'center'},
				    {field: 'REAL_AMOUNT',title: '实收金额',width: 105,align:'center'},
				    {field: 'SEND_NUM',title: '赠送数量',width: 70,align:'center'},
				    {field: 'SEND_AMOUNT',title: '赠送金额',width: 70,align:'center'},
				    {field: 'BENEFIT_PRICE',title: '让利金额',width: 70,align:'center'},
				    {field: 'SALES',title: '数量小计',width: 70,align:'center'},
				    {field: 'SALES',title: '箱数小计',width: 70,align:'center'},
				    {field: 'SALES_AMONT',title: '金额小计',width: 70,align:'center'}			   
			]];
		}else if(sumType==11){
			param[Param.CMDTYPEKEY] = 1;
			dgCol = [[
					    {field: '_rn',title: '行号',align: 'center',width: 60
					    	,formatter: function(value, row, index) {
					    		return index+1;
					    	}
						},
					    {field: 'PROD_TYPE_NAME',title: '商品类型',width: 185,align:'center'},
					    {field: 'SALES',title: '销售数量',width: 110,align:'center'},
					    {field: 'SALES_AMONT',title: '销售金额',width: 110,align:'center'},
					    {field: 'RETURN_NUM',title: '退货数量',width: 110,align:'center' },
					    {field: 'RETURN_AMOUNT',title: '退货金额',width: 105,align:'center'},
					    {field: 'REAL_AMOUNT',title: '实收金额',width: 105,align:'center'},
					    {field: 'SEND_NUM',title: '赠送数量',width: 105,align:'center'},
					    {field: 'SEND_AMOUNT',title: '赠送金额',width: 110,align:'center'},
					    {field: 'BENEFIT_PRICE',title: '让利金额',width: 110,align:'center'},
					    {field: 'SALES',title: '数量小计',width: 115,align:'center'},
					    {field: 'SALES',title: '箱数小计',width: 115,align:'center'},
					    {field: 'SALES_AMONT',title: '金额小计',width: 115,align:'center'}			   
				]];
		}
		param = Param.buildParam(param, cond);
		$('#dg').datagrid({ 
			url: CONTEXT_PATH + '/ktcount/salesSummaryList',
			queryParams: filters,
			method: 'POST',
			fit:false,
			columns:dgCol,
			
		});
	};
	// 导出excel
	var onExpExcel = function() {
		var cond = getCond();
		if (!cond) {
			return false;
		}
		var param = Param.getExportCmd();
		param = Param.buildParam(param, cond);
		if(sumType==10){
			param[Param.CMDTYPEKEY] = 0;
		}else if(sumType==11){
			param[Param.CMDTYPEKEY] = 1;
		}
		Util.doPost(url, param, function(data) {
			if (data && data.file) {
				win.open(data.file);
			} else if (data && data.stateMsg) {
				Util.msg.alert(data.stateMsg);
				return false;
			}
		});
	};
	//导出
   var daochu = function(){   
	   var startTime = $('#startTime').datebox('getValue');	
	   var endTime = $('#endTime').datebox('getValue');	
		var area = $('#area').combobox('getText');
		var shop = $('#shop').combobox('getText');
  	  exportExcel.jqgridtoExcel($(".datagrid-header"),$(".datagrid-body"),'贵州科泰易购商贸有限公司销售汇总表',startTime,endTime,area,shop);
   }
	//区域树状
	 var hbg = function(){
		 $('#win').dialog('open');
	        var setting = {
	                view: {
	                    dblClickExpand: false,
	                    showLine: true,
	                    selectedMulti: false
	                }, 
	                check: {
                        enable: true,
                        chkStyle: "checkbox",
                    },
	                data: {
	                    simpleData: {
	                        enable: true,
	                        idKey: "ZONE_CODE",
	                        pIdKey: "PARENT_ID",
	                        rootPId: '',
	                        areaCode:"ZONE_CODE"
	                    },
	    				key: {
	                        name: 'ZONE_NAME'
	    				}
	                },
	                callback: { //回调函数
	    				  onDblClick: function (treeId, treeNode) {
	    					  
	    					  var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
	    					  var node = treeObj.getSelectedNodes();//点击节点后 获取节点数据
	                       // $("#area").val(node[0].text)
	                        $('#area').combobox('setValue', node[0].text);
	                        $("#typeId").val(node[0].tId)
	                        layer.close(modalIndex)
	    				  }
	                }
	            }
	        
			var param = Param.getReadCmd();
			var cond = {};
			param = Param.buildParam(param,cond);
			param[Param.CMDTYPEKEY] = 0;
			param[Param.CMDVALUEKEY] = 1;
			param = {
					zoneCode:'',
					state:1
			}
	        $.ajax({
				//url: CONTEXT_PATH + '/ktcount/getArea',
				url: CONTEXT_PATH + '/ktcount/getArea',
				data : param,
	            type : "post",
	            dataType : "json",
	            async : false,
	            success : function(data) {
	                var dataArray = data;
	                $.fn.zTree.init($("#treeDemo"), setting, dataArray);
	                $("#authorizationInfoContentBrodBrand").removeClass("hide");
	            },
	            error : function () {
	                layer.closeAll('loading');
	                layer.msg("网络异常")
	            }
	        });
	 }
	 //区域确定
	 var bt = function(){
		 var treeObj = $.fn.zTree.getZTreeObj("treeDemo");
		 var nodes = treeObj.getCheckedNodes();
	     var areaNameVal = "",areaCodeVal = "";
	     for(var i = 0,nodesLen = nodes.length;i < nodesLen;i++){
//	    	 if(!nodes[i]["isParent"]){
		    	 areaNameVal += nodes[i]["ZONE_NAME"]+",";
		    	 areaCodeVal += nodes[i]["ZONE_CODE"]+",";
//	    	 }
	     }
	     if(nodes.length > 0){
	    	 areaNameVal = areaNameVal.substring(0,areaNameVal.length-1);
	    	 areaCodeVal = areaCodeVal.substring(0,areaCodeVal.length-1);
	     }
	     if(nodes.length == 0){
	    	 $('#area').combobox('setValue', '-');
	     }else{
	    	 $('#area').combobox('setValue', areaNameVal);
	     }
	     
	     $('#areaCode').val(areaCodeVal);
	     $('#win').dialog('close');
	     var cond = getCond2();
	     var param = Param.getReadCmd();
			param[Param.CMDTYPEKEY] = 1;
			param[Param.CMDVALUEKEY] = 1;
			param = Param.buildParam(param,cond);
			var param = {
                shopType:"'中深'",
					zoneCode:$("#areaCode").val()
			}
			$('#shop').combobox({
//				url: CONTEXT_PATH + '/ktCommon/getShopList2User',
				url: CONTEXT_PATH + '/ktcount/getShop',
				queryParams : param,
				method : 'post',
				valueField: 'SHOP_ID',
			    textField: 'SHOP_NAME',
//			    valueField: 'shopId',
//			    textField: 'shopName',
			    loader: Util.combox.loader
			}).combobox('setValue', '-');
		  
	 }
	 //商品类型树
	 var prodType = function(){
		 $('#prodTypeWin').dialog('open');
	        var setting = {
	                view: {
	                    dblClickExpand: false,
	                    showLine: true,
	                    selectedMulti: false
	                }, 
	                check: {
                        enable: true,
                        chkStyle: "checkbox",
                    },
                    data: {
                        simpleData: {
                            enable: true,
                            idKey: "TYPE_ID",
                            pIdKey: "PARENT_ID",
                            rootPId: '',
                            typeId:"TYPE_ID"
                        },
        				key: {
                            name: 'TYPE_NAME'
        				}
                    },
	            }	        
	        	var filters = {
	                "rules": [
	                    {
	                        "field": "state",
	                        "data": '1'
	                    }
	                ]
	            };
	            var queryModels = {
	                page: '1',
	                rows: '10000',
	                filters: JSON.stringify(filters), 
	            };
	            var param = Param.getReadCmd();
				var cond1 = {};
				param = Param.buildParam(param,cond1);
				param[Param.CMDTYPEKEY] = 2;
				param[Param.CMDVALUEKEY] = 1;
	        $.ajax({
	        	url: CONTEXT_PATH + '/ktcount/getGoodsType',
				data : param,
	            type : "get",
	            dataType : "json",
	            async : false,
	            success : function(data) {
	                var dataArray = data;
	                $.fn.zTree.init($("#treeDemo2"), setting, dataArray);
	                $("#authorizationInfoContentBrodBrand").removeClass("hide");
	            },
	            error : function () {
	                layer.closeAll('loading');
	                layer.msg("网络异常")
	            }
	        });
	 }
	 //商品类型确定
	 var proTypeBt = function(){
		 var treeObj = $.fn.zTree.getZTreeObj("treeDemo2");
		 var nodes = treeObj.getCheckedNodes();
	     var typeNameVal = "",typeIdVal = "";
	     for(var i = 0,nodesLen = nodes.length;i < nodesLen;i++){
	    	 if(!nodes[i]["isParent"]){
	    		 typeNameVal += nodes[i]["TYPE_NAME"]+",";
		    	 typeIdVal += nodes[i]["TYPE_ID"]+",";
	    	 }
	     }
	     if(nodes.length > 0){
	    	 typeNameVal = typeNameVal.substring(0,typeNameVal.length-1);
	    	 typeIdVal = typeIdVal.substring(0,typeIdVal.length-1);
	     }
	     if(nodes.length = 0){
	    	 $('#goodsType').combobox('setValue', '-');
	     }else{
	    	 $('#goodsType').combobox('setValue', typeNameVal);
	     }
	     $('#typeId').val(typeIdVal);
	     $('#prodTypeWin').dialog('close');
	     var typeId = {
	    		 typeId:typeIdVal
	     }
		$('#brand').combobox({
			url: CONTEXT_PATH + '/ktcount/prodTypeLinkBrand',
			queryParams : typeId,
			method : 'post',
		    valueField: 'BRAND_CODE',
		    textField: 'BRAND_NAME',
		    loader: Util.combox.loader
		}).combobox('setValue', '-');
	 }
	 
	 var shopChange = function(){  
		 $('#shop').combobox('clear');
	 }
	 var brandChange = function(){
		 $('#brand').combobox('clear');
	 }
	 var supplierChange = function(){
		 $('#supplier').combobox('clear');
	 }
	// 初始化
	$(function(){
		$('#dg').datagrid({
			fit: true,
            onLoadSuccess: compute,//页面刷新的时候触发求和的函数
			columns: [[
			    {field: '_rn',title: '行号',align: 'center',width: 30
				    	,formatter: function(value, row, index) {
				    		return index+1;
				    	}
				},
			    {field: 'PRODUCT_CODE',title: '货号',width: 110,align:'center'},
			    {field: 'SHORT_NAME',title: '品名',width: 220,align:'center',formatter:Util.dgTooltipFormat},
			    {field: 'UNIT',title: '单位',width: 40,align:'center'},
			    {field: 'STATE',title: '商品状态',width: 60,align:'center'},
			    {field: 'SPEC',title: '规格',width: 70,align:'center'},
			    {field: 'PRICE',title: '零售价',width: 50,align:'center'},
			    {field: 'CST_PRICE',title: '进价',width: 50,align:'center'},
			    {field: 'SALES',title: '销售数量',width: 70,align:'center'},
			    {field: 'SALES_AMONT',title: '销售金额',width: 70,align:'center'},
			    {field: 'RETURN_NUM',title: '退货数量',width: 70,align:'center'},
			    {field: 'RETURN_AMOUNT',title: '退货金额',width: 70,align:'center'},
			    {field: 'REAL_AMOUNT',title: '实收金额',width: 105,align:'center'},
			    {field: 'SEND_NUM',title: '赠送数量',width: 70,align:'center'},
			    {field: 'SEND_AMOUNT',title: '赠送金额',width: 70,align:'center'},
			    {field: 'BENEFIT_PRICE',title: '让利金额',width: 70,align:'center'},
			    {field: 'SALES',title: '数量小计',width: 70,align:'center'},
			    {field: 'SALES',title: '箱数小计',width: 70,align:'center'},
			    {field: 'SALES_AMONT',title: '金额小计',width: 70,align:'center'}			   
			]]
		});
		//创建条件查询下拉组件
		createCondCombox();
		// 初始化时间
		var cdate = Util.date.format(new Date(), 'yyyy-MM-dd');
		$('#startTime').datebox('setValue', cdate);
		$('#endTime').datebox('setValue', cdate);
		$(":radio").click(function(){
			var radioValue = $(this).val();
			if(radioValue>9){
				sumType = radioValue;
			}else{
				console.log("radioValue",radioValue);
				pubFunction.setTime(radioValue);
			}
	    });
		
		//获取本周的时间
		var date = new Date();
		
		// 执行查询
		$('#queryBtn').off('click').on('click', onQuery);
		// 执行导出
		//$('#exportBtn').on('click', onExpExcel); 
		$('#exportBtn').on('click', daochu); 
		// 执行退出
		$('#closeBtn').on('click', pubFunction.CloseWebPage);
		// 执行打印
		$('#printBtn').on('click', pubFunction.PrintPage);
		// 区域弹窗
		$('#quyu').on('click', hbg);
		$('#sbt').on('click',bt);
		//商品类型弹窗 
		$('#prodType').on('click', prodType);
		$('#proTypeBt').on('click', proTypeBt);
		//隐藏areaCode                  
	    //$('#areaCode').next(".combo").hide(); 
		//店铺、品牌、供应商 获得焦点清除输入框
		$('#tdShop').on('change',shopChange);
		//$('#tdShop').on('click',shopChange);
		$('#tdBrand').on('change',brandChange);
		//$('#tdBrand').on('click',brandChange); 
		$('#tdSupplier').on('change',supplierChange);
		//$('#tdSupplier').on('click',supplierChange);
		
	});

    function compute() {
        var rows = $("#dg").datagrid("getRows")//获取当前的数据行
        //计算总和
        var price = 0, cst_price = 0 
            , sales = 0, sales_amont = 0, 
            return_num = 0, return_amount=0,send_num=0,
            send_amount=0,benefit_price=0,is_heji = 0
            real_amount = 0;
        for (var i = 0; i < rows.length; i++) {
            price += parseFloat(rows[i]['PRICE']);
            cst_price += parseFloat(rows[i]['CST_PRICE']);
            sales += parseFloat(rows[i]['SALES']);
            sales_amont += parseFloat(rows[i]['SALES_AMONT']);
        	return_num += parseFloat(rows[i]['RETURN_NUM']);
            return_amount += parseFloat(rows[i]['RETURN_AMOUNT']);
            real_amount += parseFloat(rows[i]['REAL_AMOUNT']);
            send_num +=parseFloat(rows[i]['SEND_NUM']);
            send_amount +=parseFloat(rows[i]['SEND_AMOUNT']); 
            benefit_price +=parseFloat(rows[i]['BENEFIT_PRICE']);

            if (rows[i]['areaname'] == "<b>合计：</b>") {
                is_heji = 1;
            }
         }
        if (is_heji == 0) {
        	//新增一行显示统计信息 PROD_TYPE_NAME
            if(sumType==10){
                $("#dg").datagrid('appendRow', {
                    PRODUCT_CODE: '<b>合计：</b>',
                    PRICE: price.toFixed(2),
    				CST_PRICE: cst_price.toFixed(2),
                    SALES:sales.toFixed(2),
                    SALES_AMONT:sales_amont.toFixed(2),
                    RETURN_NUM:return_num.toFixed(2),
                    RETURN_AMOUNT:return_amount.toFixed(2),
                    REAL_AMOUNT:real_amount.toFixed(2),
                    SEND_NUM:send_num.toFixed(2),
                    SEND_AMOUNT:send_amount.toFixed(2),
                    BENEFIT_PRICE:benefit_price.toFixed(2),
                });
            }else if(sumType==11){
                $("#dg").datagrid('appendRow', {
                	PROD_TYPE_NAME: '<b>合计：</b>',
                    PRICE: price.toFixed(2),
    				CST_PRICE: cst_price.toFixed(2),
                    SALES:sales.toFixed(2),
                    SALES_AMONT:sales_amont.toFixed(2),
                    RETURN_NUM:return_num.toFixed(2),
                    RETURN_AMOUNT:return_amount.toFixed(2),
                    REAL_AMOUNT:real_amount.toFixed(2),
                    SEND_NUM:send_num.toFixed(2),
                    SEND_AMOUNT:send_amount.toFixed(2),
                    BENEFIT_PRICE:benefit_price.toFixed(2),
                });
            }
        }
    }
})(jQuery, window, document);
